package com.ys.fmms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ys.fmms.pojo.*;
import com.ys.fmms.service.*;
import com.ys.fmms.util.Result;
import com.ys.fmms.util.TimeUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "处理设备信息")
@RequestMapping("/fmms/nodeController")
public class NodeController {
    @Autowired
    private ShearWaveService shearWaveService;

    @Autowired
    private MetalDetectionService metalDetectionService;

    @Autowired
    private LongitudinalWaveService longitudinalWaveService;

    @Autowired
    private LaserRangingService laserRangingService;

    @Autowired
    private HumanBodyInfraredService humanBodyInfraredService;

    @Autowired
    private EnvironmentalNoiseService environmentalNoiseService;

    @Autowired
    private NodeListService nodeListService;

    @Autowired
    private NodeResourceListService nodeResourceListService;

    @Autowired
    private SystemStatusService systemStatusService;

    @GetMapping("/getNodeList")
    @Operation(summary = "获取节点列表")
    public Result getNodeList() {
        List<NodeList> nodeList = nodeListService.getNodeList();
        return Result.ok(nodeList);
    }

    @GetMapping("/getNodeResourceList/{id}")
    @Operation(summary = "根据id获取当前节点的资源信息列表")
    public Result getNodeResourceList(@PathVariable(value = "id") String id) {
        List<NodeResourceList> nodeResourceLists = nodeResourceListService.getNodeResourceList(id);
        return Result.ok(nodeResourceLists);
    }

    @GetMapping("/getSystemInfoAndSensorInfo")
    @Operation(summary = "获取系统状态和传感器状态")
    public Result getSystemInfoAndSensorInfo() {
        SystemStatus systemStatus = systemStatusService.getInfo();
        ShearWave shearWave = shearWaveService.getLast();
        MetalDetection metalDetection = metalDetectionService.getLast();
        LongitudinalWave longitudinalWave = longitudinalWaveService.getLast();
        LaserRanging laserRanging = laserRangingService.getLast();
        HumanBodyInfrared humanBodyInfrared = humanBodyInfraredService.getLast();
        EnvironmentalNoise environmentalNoise = environmentalNoiseService.getLast();
        List<Object> list = new ArrayList<>();
        list.add(shearWave);
        list.add(systemStatus);
        list.add(metalDetection);
        list.add(longitudinalWave);
        list.add(laserRanging);
        list.add(humanBodyInfrared);
        list.add(environmentalNoise);
        return Result.ok(list);
    }

    @GetMapping("/getAnalysisData")
    @Operation(summary = "获取数据分析数据")
    public Result getAnalysisData() {
        List<DataAnalysis> analysisList = metalDetectionService.getAnalysisList();
        return Result.ok(analysisList);
    }

    @GetMapping("/getNewStatusData")
    @Operation(summary = "获取节点当前的数据信息列表")
    public Result getNewStatusData() {
        List<NodeList> nodeLists = nodeListService.getNewStatusDataList();
        return Result.ok(nodeLists);
    }

    @GetMapping("/getNodeManagerList/{pageNo}/{pageSize}")
    @Operation(summary = "获取节点列表并且支持模糊和分页查询")
    public Result getNodeManagerList(
            @PathVariable(value = "pageNo") Long pageNo,
            @PathVariable(value = "pageSize") Long pageSize,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String status
    ) {
        Page<NodeList> page = new Page<>(pageNo, pageSize);
        IPage<NodeList> nodeListIPage = nodeListService.getNodeListByFuzzySearch(page, type, status);
        return Result.ok(nodeListIPage);
    }

    @PostMapping("/updateCurrentFacilityInfo")
    @Operation(summary = "保存上传的设备图片路径")
    public Result updateCurrentFacilityInfo(@RequestBody String imageUrl, @RequestParam String id) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Map map = objectMapper.readValue(imageUrl, Map.class);
        String imageUrl1 = (String) map.get("imageUrl");
        QueryWrapper<NodeList> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        NodeList nodeList = new NodeList();
        nodeList.setUpdateTime(TimeUtils.currentDate());
        nodeList.setPicPath(imageUrl1);
        nodeListService.saveOrUpdate(nodeList, wrapper);
        return Result.ok();
    }

    @PutMapping("/addOrUpdateNodeListById/{id}")
    @Operation(summary = "增加或根据id修改NodeList")
    public Result addOrUpdateNodeListById(@PathVariable(required = false) String id,
                                          @RequestBody NodeList nodeObj) {
        nodeListService.addOrUpdateNodeListById(id, nodeObj);
        return Result.ok();
    }

    @GetMapping("/getFacilityNameList/{facility}")
    @Operation(summary = "获取设备字典")
    public Result getFacilityNameList(@PathVariable String facility) {
        List<String> stringList = nodeListService.getFacilityNameList(facility);
        return Result.ok(stringList);
    }

    @GetMapping("/getNodeListById/{id}")
    @Operation(summary = "通过id获取Nodelist对象")
    public Result getNodeListById(@PathVariable String id) {
        NodeList nodeList = nodeListService.getById(id);
        return Result.ok(nodeList);
    }
}
